package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationResDTO;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {

    @Resource
    private AgencyCertificationMapper agencyCertificationMapper;

    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    //("机构认证，添加信息")
    @Override
    public AgencyCertificationResDTO certificationAudit(AgencyCertificationAuditAddReqDTO auditAddReqDTO) {
        AgencyCertification agencyCertification = lambdaQuery().eq(AgencyCertification::getIdNumber, auditAddReqDTO.getIdNumber()).one();
        if (!ObjectUtil.isNull(agencyCertification)){
            throw new BadRequestException("该统一社会信用代码已经存在");
        }
        AgencyCertification agencyCertification1 = new AgencyCertification();
        BeanUtil.copyProperties(auditAddReqDTO,agencyCertification1); //拷贝对象

        // CertificationStatusEnum
        //    INIT(0, "初始态"),
        //    PROGRESSING(1, "认证中"),
        //    SUCCESS(2, "认证成功"),
        //    FAIL(3, "认证失败");
        agencyCertification1.setCertificationStatus(CertificationStatusEnum.INIT.getStatus());
        agencyCertification1.setCertificationTime(LocalDateTime.now()); //设置认证时间
        Long insert = agencyCertificationMapper.addCertificationAudit(agencyCertification1);
        if (insert<1){
            throw new CommonException("添加失败");
        }
        //根据插入数据的id 获取插入后的数据
        AgencyCertification agencyCertification2 = baseMapper.selectById(agencyCertification1.getId());
        //数据拷贝到 响应dto
        AgencyCertificationResDTO agencyCertificationResDTO = BeanUtils.toBean(agencyCertification2, AgencyCertificationResDTO.class);

        return agencyCertificationResDTO;

    }

    //("获取机构认证信息列表")
    @Override
    public PageResult<AgencyCertification> getPage(AgencyCertificationAuditPageQueryReqDTO pageQueryReqDTO) {
               PageResult<AgencyCertification> pageResult =PageHelperUtils.selectPage(pageQueryReqDTO,
                () ->baseMapper.queryList(pageQueryReqDTO.getName(),pageQueryReqDTO.getLegalPersonName()
                        ,pageQueryReqDTO.getCertificationStatus(),pageQueryReqDTO.getAuditStatus()));

        return pageResult;
    }


}
